package cn.gailvlun.gll.presentation.apartment.room;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.chatlib.db.ChatDB2;
import cn.gailvlun.gll.databinding.ActivityMatchSuccessBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.Member;
import cn.gailvlun.gll.net.base.Profile;
import cn.gailvlun.gll.presentation.apartment.ApartmentDetailActivity;
import cn.gailvlun.gll.presentation.base.App;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.chat.ChatActivity;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.realm.Realm;



public class MatchSuccessActivity extends BaseActivity {

    
    public static void openActivity(Context context, int cpMemberId) {
        Intent intent = new Intent(context, MatchSuccessActivity.class);
        intent.putExtra("cpMemberId", cpMemberId);
        context.startActivity(intent);
    }

    private ActivityMatchSuccessBinding mBinding;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_match_success);
        mBinding.setPresenter(new Presenter());
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        int cpMemberId = getIntent().getIntExtra("cpMemberId", -1);

        final Dialog dialog = DialogUtil.showIndeterminate(mContext);
        HttpMethods.getMemberService().getMember(null, cpMemberId, null)
                .compose(RxUtil.<Member>applyScheduler())
                .compose(this.<Member>bindToLifecycle())
                .subscribe(new HttpSubscriber<Member>() {
                    @Override
                    protected void onSuccess(Member response) {
                        mBinding.setItem(response.getOwner());
                        dialog.dismiss();
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable Member response, int code) {
                        dialog.dismiss();
                        DialogUtil.showError(mContext, errMsg);
                    }
                });
    }

    @Override
    public void onBackPressed() {
        ApartmentDetailActivity.openActivity(mContext);
    }

    public class Presenter {
        public void onPrivateChat(final Profile item) {
            Realm realm = Realm.getInstance(ChatDB2.INSTANCE.getConfiguration());
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    realm.insertOrUpdate(item);
                }
            });
            realm.close();

            ChatActivity.openActivity(mContext, "user:" + item.getId());
            finish();
        }
    }

    public static class Util {
        public static Drawable getUserSex(Profile item) {
            if (item == null) {
                return null;
            }
            if (item.isGender()) {
                
                return App.getInstance().getResources().getDrawable(R.drawable.ic_match_male);
            } else {
                
                return App.getInstance().getResources().getDrawable(R.drawable.ic_match_female);
            }
        }
    }
}
